package proxy 
{
	import data.DataMonitor;
	import debug.ASSERT;
	import nsdecl.ns_control_model;
	import nsdecl.ns_fileurl_const;
	import nsdecl.ns_vname_decl;
	import common.IData;
	import common.Struct;
	import common.StructGroup;
	import pool.Pool;
	import vndecl.*;
	/**
	 * ...
	 * @author anod
	 */
	internal class StructGroupProxy implements IDataProxy 
	{
		use namespace ns_fileurl_const;
		use namespace ns_control_model;
		use namespace ns_vname_decl;
		
	///---------------------------------------
		private var refStruct:Struct;
		private var mDirty:Boolean;
		private var mIdGen:int;
		
		public function StructGroupProxy( struct:Struct ) 
		{
			super();
			refStruct = struct;
			DataMonitor.updateData( refGroupVector, vname );
		}
		
		private function get refGroupVector():Vector.<StructGroup>
		{
			return refStruct.mGroupVector;
		}
		
		/* INTERFACE proxy.IDataProxy */
		
		public function init():void
		{
			mIdGen = refGroupVector.length;
		}
		
		public function get vname():String 
		{
			return VNGM.PROXY_GROUP;
		}
		
		public function get isDirty():Boolean
		{
			return mDirty;
		}
		internal function clearDirty():void
		{
			mDirty = false;
		}
		
		public function insert(v:IData):int 
		{
			ASSERT( v is StructGroup, "只接受“分组”数据" );
			
			var newentry:StructGroup;
			
			// 检查是否重复
			newentry = match( v as StructGroup );
			if ( newentry != null ) return -1;
			
			// 补全数据
			newentry      = new StructGroup( GenID() );
			newentry.name = StructGroup(v).name;
			newentry.copyMembersFrom( StructGroup(v).members );
			
			// 加入到队列中
			(mDirty = true) && refGroupVector.push( newentry ) && DataMonitor.notify( vname );
			return newentry.ID;
		}
		
		public function remove(id:int):int 
		{
			var index_to_del:int = -1;
			refGroupVector.some(
				function( entry:StructGroup, idx:int, db:Vector.<StructGroup> ):Boolean
				{
					var find:Boolean = ( entry.ID == id );
					if ( find ) index_to_del = idx;
					return find;
				}
			);
			(id >= 0) && refGroupVector.splice(index_to_del,1) && (mDirty = true) && DataMonitor.notify(  vname );
			return index_to_del < 0 ? index_to_del : id;
		}
		
		public function update(id:int, v:IData):int 
		{
			ASSERT( v is StructGroup, "只接受“分组”数据" );
			
			var entry:StructGroup = getByID( id ) as StructGroup;
			if ( entry == null ) return -1;
			
			entry.name = StructGroup(v).name;
			entry.copyMembersFrom( StructGroup(v).members );
			
			(mDirty = true) && DataMonitor.notify( vname );
			return id;
		}
		
		public function getByID( id:int ):IData
		{
			ASSERT( id >= 0, "合法ID必定是非负数" );
			for each( var group:StructGroup in refGroupVector )
			{
				if ( group.ID == id )
					return group;
			}
			return null;
		}
		
		/**
		 * 从已有的队列refGroupVector中找到和传入项想匹配的数据
		 * @param	v - 需要对应匹配的项
		 * @param   id - 如果非负，表示只匹配id
		 * @return  找到了想匹配的项，返回对应对象，否则返回null
		 */
		protected function match( v:StructGroup ):StructGroup
		{
			for each( var group:StructGroup in refGroupVector )
			{
				if ( group.name == v.name )
					return group;
			}
			return null;
		}
		
		protected function GenID():int
		{
			return mIdGen++;
		}
		
		///同步人员数据
		///-----------------------------------
		internal function recalcStaffReference():void
		{
			for each( var group:StructGroup in refGroupVector )
			{
				var members:Array = group.members;
				var members_new:Array = [];
				for each( var id:int in members )
				{
					if ( DataAccess.instance.queryStaffInterf().getByID(id) != null )
					{
						members_new.push( id );
					}
				}
				(members_new.length < members.length) && group.copyMembersFrom( members );
			}
		}
	}

}